package tmhprediction.eval;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;

import tmhprediction.main.TMHResultMap;

public class WriteOutput
{
	
	public static void writeObservedAndPredicted(TMHResultMap map, String outf)
	{
		File out = new File(outf);
		
		HashMap<String, ArrayList<short[]>> parsedMap = TMHHelixEvaluator.finalHelixEvaluation(map);
		
		try {
			FileUtils.writeStringToFile(out, "#>id\n#OBS\n#PRED\nCONFIDENCE_FOR_TMH\n", true);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		for (String protein : parsedMap.keySet())
		{			
			try
			{
				FileUtils.writeStringToFile(out, ">"+protein+"\n", true);
				FileUtils.writeStringToFile(out, getStringRepresentation(parsedMap.get(protein), TMHHelixEvaluator.INDEX_OBS) + "\n", true);
				FileUtils.writeStringToFile(out, getStringRepresentation(parsedMap.get(protein), TMHHelixEvaluator.INDEX_PRED) + "\n", true);
				FileUtils.writeStringToFile(out, getStringRepresentation(parsedMap.get(protein), TMHHelixEvaluator.INDEX_CONFIDENCE) + "\n", true);
			}
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
	}
	
	private static String getStringRepresentation(ArrayList<short[]> data, int index)
	{
		Iterator<short[]> iter = data.iterator();
		String result = "";
		
		while(iter.hasNext())
		{
			result = result + iter.next()[index];
		}
		return result;
	}

}
